package com.briup.service.impl;

import com.briup.bean.User;
import com.briup.dao.UserMapper;
import com.briup.service.IUserService;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

@Service
public class UserServiceImpl implements IUserService {
	@Autowired
	private UserMapper userMapper;

	@Override
	public void register(User user) {
		String name = user.getName();
		//名字不能为空 且不能为""且不能为"    "
		if(StringUtils.hasLength(name)) {
			throw new RuntimeException("名字不能为空");
		}
		//当前用户是否存在
		User userFromDB = findUserByName(name);
		if(userFromDB != null) {
			//用户名已经存在
			throw new RuntimeException("用户名存在");
		}
		userMapper.saveUser(user);//id
	}

	@Override
	public User findUserByName(String name) {
		User user = userMapper.findUserByName(name);
		return user;
	}

	@Override
	public User login(String name, String password) {
		if(!StringUtils.hasLength(name)) {
			throw new RuntimeException("名字不能为空");
		}
		//当前用户是否存在
		User userFromDB = findUserByName(name);
		if(userFromDB == null || !userFromDB.getPassword().equals(password)) {
			throw new RuntimeException("用户名或密码不正确");
		}
		return userFromDB;
	}

}
